package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapScreenshot;
import com.guodi.map.service.IMapScreenshotService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @作者: 林楷
 * @日期: 2021/11/10
 * @描述: 截图保存信息
 */
@RestController
@RequestMapping("/MapScreenshot")
@Api(value = "API - MapScreenshotController", tags = "截图保存信息")
public class MapScreenshotController extends BaseController {

    @Resource
    private IMapScreenshotService screenshotService;

    /**
     * @作者: 林楷 2021/11/10
     * @描述: 查询
     * @参数:
     * @返回:
     */
    @PostMapping("/listByEntity")
    @ApiOperation(value = "根据实体属性分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录大小", required = true, dataType = "long", paramType = "query")
    })
    public R<Page> listByEntity(@ApiIgnore Page page, @ApiIgnore MapScreenshot screenshot){
        return R.data(screenshotService.page(page, new LambdaQueryWrapper<MapScreenshot>().orderByDesc(MapScreenshot::getCreateTime)));
    }
    /**
     * @作者: 林楷 2021/11/10
     * @描述: 根据ID查询
     * @参数:
     * @返回:
     */
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "query")
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        return R.data(screenshotService.getById(id));
    }
    /**
     * @作者: 林楷 2021/11/10
     * @描述: 保存上传截图
     * @参数:
     * @返回:
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(MapScreenshot screenshot, MultipartFile file){
        if(file == null) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "没有上传截图");
        }
        if(!file.getOriginalFilename().endsWith(".png") && !file.getOriginalFilename().endsWith(".jpg")) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "上传png,jpg格式截图");
        }
        if(StringUtils.isBlank(screenshot.getRange())) {
            return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "没有填写空间范围");
        }
        screenshot.setCreator(getUser().getUserId().toString());
        screenshot.setCreateTime(new Date());
        try {
            screenshotService.save(screenshot, file);
            return R.data(screenshot);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.fail(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "保存失败");
    }
    /**
     * @作者: 林楷 2021/11/10
     * @描述: 根据主键删除
     * @参数:
     * @返回:
     */
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "query")
    public R delete(String id){
        if(screenshotService.deleteById(id)) {
            return R.status(true);
        } else {
            return R.data(StatusCode.REQUEST_CODE_OPERATION_FAILURE, "删除失败");
        }
    }

}
